package com.example.websockettest.controller;

import com.example.websockettest.config.OneToManyWebSocket;
import com.example.websockettest.config.OneWebSocket;
import com.example.websockettest.config.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.websocket.Session;
import java.io.IOException;
import java.util.Map;

/* ━━━━━━佛祖保佑━━━━━━
 *                  ,;,,;
 *                ,;;'(    社
 *      __      ,;;' ' \   会
 *   /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 *,;' \    /-.,,(   ) \    码
 *     ) /       ) / )|    农
 *     ||        ||  \)
 *     (_\       (_\
 * ━━━━━━永无BUG━━━━━━
 * @author ：zjq
 * @date ：2020/12/7 20:13
 * @description: TODO
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催1
 */
@RestController
@RequestMapping("a")
public class SocketController {




    @Autowired
    private WebSocketServer webSocketServer;

    /**
     * 给指定用户推送消息
     *
     * @param userName 用户名
     * @param message  消息
     * @throws IOException
     */
    @RequestMapping(value = "/socket", method = RequestMethod.GET)
    public void testSocket1(@RequestParam String userName, @RequestParam String message) {
        webSocketServer.sendInfo(userName, message);
    }

    /**
     * 给所有用户推送消息
     *
     * @param message 消息
     * @throws IOException
     */
    @RequestMapping(value = "/socket/all", method = RequestMethod.GET)
    public void testSocket2(@RequestParam String message) {
        try {
            webSocketServer.onMessage(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // http://localhost:18092/a/socket/allJ
    @RequestMapping(value = "/socket/allJ", method = RequestMethod.POST)
    public void testSocketPost(@RequestBody Map<String,Object> map) {
        try {
            webSocketServer.onMessage(map.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Autowired
    private OneToManyWebSocket oneToManyWebSocket;

    @RequestMapping(value = "/socket2/all", method = RequestMethod.GET)
    public void testSocketall(@RequestParam String message) {
        try {
            oneToManyWebSocket.sendMessage(message, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
